package com.dreamoo.service.factory;

import com.dreamoo.common.enums.FileStorageEnum;
import com.dreamoo.service.strategy.image.FileStoragStrategy;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * @author 山顶洞人郑某
 * @className FileStorageFactory.java 文件存储方式工厂
 * @date 2025 05 15
 */
@Component
public class FileStorageFactory implements ApplicationContextAware {

    private Map<FileStorageEnum, FileStoragStrategy> map = new HashMap<>();
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext;
    }
    @PostConstruct
    public void init() {
        Map<String, FileStoragStrategy> beans = applicationContext.getBeansOfType(FileStoragStrategy.class);
        beans.forEach((s, fileStoragStrategy) ->
                map.put(fileStoragStrategy.getType(), fileStoragStrategy));
    }

    public FileStoragStrategy getFileStoragStrategy(FileStorageEnum fileStorageEnum) {
        return map.get(fileStorageEnum);
    }

}
